Conditions | 7 |
Paths | 12294 |
Total Lines | 140 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import xss from 'xss' |
||
17 | var route = function(req, res, next) { |
||
18 | var filePath = req.originalUrl.replace('/abe', '') |
||
19 | if (filePath === '' || filePath === '/') { |
||
20 | filePath = null |
||
21 | } |
||
22 | |||
23 | if(filePath != null){ |
||
24 | var testXSS = xss(filePath, { |
||
25 | whiteList: [], |
||
26 | stripIgnoreTag: true |
||
27 | }) |
||
28 | if(testXSS !== filePath){ |
||
29 | filePath = testXSS |
||
30 | } |
||
31 | } |
||
32 | |||
33 | abeExtend.hooks.instance.trigger('beforeRoute', req, res, next) |
||
34 | if(typeof res._header !== 'undefined' && res._header !== null) return |
||
|
|||
35 | |||
36 | var isHome = true |
||
37 | var jsonPath = null |
||
38 | var linkPath = null |
||
39 | var template = null |
||
40 | var fileName = null |
||
41 | var folderPath = null |
||
42 | |||
43 | let p = new Promise((resolve) => { |
||
44 | |||
45 | if(filePath != null) { |
||
46 | fileName = filePath.split('/') |
||
47 | fileName = fileName[fileName.length-1].replace(`.${config.files.templates.extension}`, '') |
||
48 | |||
49 | folderPath = filePath.split('/') |
||
50 | folderPath.pop() |
||
51 | folderPath = folderPath.join('/') |
||
52 | |||
53 | isHome = false |
||
54 | |||
55 | var filePathTest = cmsData.revision.getDocumentRevision(filePath) |
||
56 | if(typeof filePathTest !== 'undefined' && filePathTest !== null) { |
||
57 | jsonPath = filePathTest.path |
||
58 | linkPath = filePathTest.abe_meta.link |
||
59 | template = filePathTest.abe_meta.template |
||
60 | } |
||
61 | |||
62 | if(jsonPath === null || !coreUtils.file.exist(jsonPath)) { |
||
63 | res.redirect('/abe/') |
||
64 | return |
||
65 | } |
||
66 | |||
67 | editor(template, jsonPath, linkPath) |
||
68 | .then((result) => { |
||
69 | resolve(result) |
||
70 | }).catch(function(e) { |
||
71 | console.error(e) |
||
72 | }) |
||
73 | }else { |
||
74 | resolve({ |
||
75 | obj: {}, |
||
76 | manager: {} |
||
77 | }) |
||
78 | } |
||
79 | }).catch(function(e) { |
||
80 | console.error(e) // "oh, no!" |
||
81 | }) |
||
82 | |||
83 | p.then((result) => { |
||
84 | var obj = result |
||
85 | var manager = {} |
||
86 | |||
87 | manager.home = { |
||
88 | files: Manager.instance.getList() |
||
89 | } |
||
90 | |||
91 | manager.list = Manager.instance.getStructureAndTemplates() |
||
92 | manager.editConfig = req.app.get('config') |
||
93 | manager.config = JSON.stringify(config) |
||
94 | |||
95 | var _hasBlock = (obj) ? obj.hasBlock : false |
||
96 | var _hasSingleBlock = (obj) ? obj.hasSingleBlock : false |
||
97 | var _preview = (filePath) ? '/abe/page/' + req.params[0] + `?filePath=${req.query.filePath}` : false |
||
98 | var _form = (obj) ? obj.form : false |
||
99 | var _json = (obj) ? obj.json : false |
||
100 | var _text = (obj) ? obj.text : false |
||
101 | // var _file = (tplUrl) ? tplUrl.draft.file : false |
||
102 | var _filePath = (filePath) ? filePath : false |
||
103 | if (_filePath) { |
||
104 | _filePath = '/' + _filePath.replace(/^\/+/, '') |
||
105 | } |
||
106 | |||
107 | var pageHtml = '' |
||
108 | if(typeof _json !== 'undefined' && _json !== null |
||
109 | && typeof _json.abe_meta !== 'undefined' && _json.abe_meta !== null) { |
||
110 | |||
111 | var text = cmsTemplates.template.getTemplate(_json.abe_meta.template) |
||
112 | var page = new Page(_json.abe_meta.template, text, _json, false) |
||
113 | pageHtml = page.html.replace(/"/g, '"').replace(/'/g, '\'').replace(/<!--/g, '<ABE!--').replace(/-->/g, '--ABE>') |
||
114 | } |
||
115 | |||
116 | var editorWidth = '33%' |
||
117 | req.headers && req.headers.cookie.split(';').forEach(function(cookie) { |
||
118 | var parts = cookie.match(/(.*?)=(.*)$/) |
||
119 | if(parts[1] === 'editorWidth') editorWidth = parts[2] |
||
120 | }) |
||
121 | var EditorVariables = { |
||
122 | pageHtml: pageHtml, |
||
123 | isHome: isHome, |
||
124 | abeUrl: '/abe/', |
||
125 | test: JSON.stringify(locale), |
||
126 | text: locale, |
||
127 | preview: _preview, |
||
128 | filename: fileName, |
||
129 | folderPath: folderPath, |
||
130 | hasSingleBlock: _hasSingleBlock, |
||
131 | hasBlock: _hasBlock, |
||
132 | form: _form, |
||
133 | json: _json, |
||
134 | config: config, |
||
135 | Locales: coreUtils.locales.instance.i18n, |
||
136 | manager: manager, |
||
137 | express: { |
||
138 | res: res, |
||
139 | req: req |
||
140 | }, |
||
141 | abeVersion: pkg.version, |
||
142 | nonce: '\'nonce-' + res.locals.nonce + '\'', |
||
143 | editorWidth: editorWidth |
||
144 | } |
||
145 | EditorVariables = abeExtend.hooks.instance.trigger('afterVariables', EditorVariables) |
||
146 | |||
147 | if (filePath != null && filePath.indexOf('.json') > -1) { |
||
148 | res.set('Content-Type', 'application/json') |
||
149 | res.send(JSON.stringify(_json)) |
||
150 | }else { |
||
151 | res.render(config.abeEngine, EditorVariables) |
||
152 | } |
||
153 | }).catch((e) => { |
||
154 | console.log('error', e) |
||
155 | }) |
||
156 | } |
||
157 | |||
158 | export default route |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.